package com.john.bms.service.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.john.bms.mapper.system.MenuMapper;
import com.john.bms.mapper.system.RoleMapper;
import com.john.bms.model.system.menu.SysMenu;
import com.john.bms.model.system.role.RolePage;
import com.john.bms.model.system.role.RolePower;
import com.john.bms.model.system.role.SysRole;
import com.john.bms.model.system.user.SysUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author:张家小帅哥
 * @Date:2020/5/30
 * @Version:1.0
 * @Description:TODO
 */
@Service
public class RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private MenuMapper menuMapper;

    public List<Map<String, String>> getRoles() {
        List<Map<String, String>> sysRoles = roleMapper.selectRoleNameList();
        return sysRoles;
    }

    public Map<String, Object> getRoleNames() {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, String>> roleList = roleMapper.selectRoleNameAndIdList();
        List<Map<String, Object>> sysMenus = menuMapper.selectName();
        map.put("roleList", roleList);
        map.put("sysMenus", sysMenus);
        return map;
    }

    public PageInfo<SysRole> getRoleList(RolePage rolePage) {
        PageHelper.startPage(rolePage.getPageNum(), rolePage.getPageSize());
        List<SysRole> sysRoles = roleMapper.selectPageList(rolePage);
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoles);
        return pageInfo;
    }

    public boolean updateRoleState(int id, int state) {
        int i = roleMapper.updateState(id, state);
        return i == 1 ? true : false;
    }

    public boolean deleteRole(int rId) {
        int i = roleMapper.deleteRole(rId);
        return i == 1 ? true : false;
    }

    public boolean insertRole(SysRole sysRole) {
        sysRole.setState(1);
        sysRole.setCreateTime(new Date());
        int insert = roleMapper.insert(sysRole);
        return insert == 1 ? true : false;
    }

    public SysRole getRoleInfo(int rId) {
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("r_id", rId);
        SysRole sysRole = roleMapper.selectOne(wrapper);
        return sysRole;
    }

    public boolean updateRole(SysRole sysRole) {
        sysRole.setUpdateTime(new Date());
        int i = roleMapper.updateRole(sysRole);
        return i == 1 ? true : false;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean authorize(RolePower rolePower) {
        //先删除原来的权限，然后在添加
        roleMapper.deleteByPrimaryKey(rolePower.getRId());
        List<String> menuIdList = Arrays.asList(rolePower.getRules());
        roleMapper.insertRoleMenu(rolePower.getRId(), menuIdList);
        return true;
    }

    public List<Integer> getPowerList(int rId) {
        List<Integer> list = roleMapper.selectRoleMenuList(rId);
        return list;
    }

}
